﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BarcodeDelay.view
{
    public partial class FrmArrivalSetting : Form
    {
        public FrmArrivalSetting()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if(tbTime.Text.Trim().Length<1)
            {
                MessageBox.Show("时间设置不能为空");
                return;
            }
            if(!isRightTimeFormat(tbTime.Text.Trim()))
            {
                MessageBox.Show("时间格式不正确,应该为类似23:06 这种格式");
                return;
            }
            writeTbFile(tbPrior.Text.Trim(), tbTime.Text.Trim());
            

            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private bool isRightTimeFormat(string strTime)
        {
            bool res = false;
            try
            {
                DateTime dt= DateTime.Parse(strTime);
                res = true;
            }
            catch(Exception ex)
            {
                res = false;
            }
            return res;
        }

        private void writeTbFile(string strSiteName,string time)
        {
            try
            {
                string delFile = System.Environment.CurrentDirectory + "\\" + StoParams.Instance.siteArrTxtPath;
                File.Delete(delFile);
                FileStream bFile = new FileStream(delFile, FileMode.Create);
                StreamWriter swb = null;
                if (swb == null)
                {
                    swb = new StreamWriter(bFile);
                }
                StoParams.Instance.arrival_priorSite = strSiteName;
                StoParams.Instance.arrival_priorSite_SacnEndTime = time;
                swb.WriteLine("regsite " + strSiteName);
                swb.WriteLine("time " + time);
                

                swb.Close();

            }
            catch (Exception ex)
            {

            }
        }

        private void FrmArrivalSetting_Load(object sender, EventArgs e)
        {
            this.tbPrior.Text = StoParams.Instance.arrival_priorSite;
            this.tbTime.Text = StoParams.Instance.arrival_priorSite_SacnEndTime;
        }
    }
}
